package com.cxs.service.impl;

import com.cxs.enums.HospitalErrorResultEnum;
import com.cxs.exception.HospitalException;
import com.cxs.mapper.MenuMapper;
import com.cxs.mapper.RoleMapper;
import com.cxs.pojo.Menu;
import com.cxs.pojo.Role;
import com.cxs.pojo.RoleExample;
import com.cxs.service.RoleService;
import com.cxs.utils.QueryUtil;
import com.cxs.vo.MenuTreeVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/*
 * @Author:cxs
 * @Date:2021/10/18  16:31
 * @Version 1.0
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
@Slf4j
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Role> selectAll() {
        return roleMapper.selectByExample(null);
    }

    @Override
    public PageInfo<Role> selectAllCondition(Role role, Integer pageNum, Integer pageSize) {

        RoleExample example = new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();

        if(!StringUtils.isEmpty(role.getRoleName())){
            criteria.andRoleNameLike(QueryUtil.createLikeResult(role.getRoleName()));
        }

        if(!StringUtils.isEmpty(role.getRoleDesc())){
            criteria.andRoleDescLike(QueryUtil.createLikeResult(role.getRoleDesc()));
        }

        if(!StringUtils.isEmpty(role.getAvailable())){
            criteria.andAvailableEqualTo(role.getAvailable());
        }

        PageHelper.startPage(pageNum,pageSize);
        List<Role> roleList = roleMapper.selectByExample(example);

        return new PageInfo<Role>(roleList);
    }

    @Override
    public void update(Role role) {
        int result = roleMapper.updateByPrimaryKeySelective(role);
        if(result != 1){
            log.error("角色信息修改失败,role = {}",role);
            throw new HospitalException(HospitalErrorResultEnum.ROLE_UPDATE_ERROR);
        }
    }

    @Override
    public void insert(Role role) {
        int result = roleMapper.insertSelective(role);
        if(result != 1){
            log.error("角色添加失败,role = {}",role);
            throw new HospitalException(HospitalErrorResultEnum.ROLE_INSERT_ERROR);
        }
    }

    @Override
    public void delete(Integer roleId) {
        int result = roleMapper.deleteByPrimaryKey(roleId);
        if(result != 1){
            log.error("角色删除失败,roleId = {}",roleId);
            throw new HospitalException(HospitalErrorResultEnum.ROLE_DELETE_ERROR);
        }
    }

    @Override
    public List<MenuTreeVO> loadRoleMenu(Integer roleId) {
        // 查询所有权限菜单
        List<Menu> menuAllList = menuMapper.selectByExample(null);
        // 查询该角色拥有的权限菜单
        List<Menu> menuList = roleMapper.selectMenuByRoleId(roleId);
        // 创建结果VO
        List<MenuTreeVO> treeVOList = new ArrayList<>();
        // 处理数据
        for (Menu menu : menuAllList) {
            MenuTreeVO treeVO = new MenuTreeVO();
            BeanUtils.copyProperties(menu,treeVO);
            // 封装是否展开
            if(menu.getSpread() == 1){
                treeVO.setSpread(true);
            }
            // 判断该角色是否拥有此权限
            if(menuList.contains(menu)){
                treeVO.setCheckArr("1");
            }
            treeVOList.add(treeVO);
        }
        return treeVOList;
    }

    @Override
    @Transactional
    public void saveRoleMenu(Integer roleId, Integer[] ids) {
        // 1. 删除角色原有的权限
        int count = roleMapper.deletehavedMenuByroleId(roleId);
        // 2. 给角色重新赋予权限
        if(ids == null || ids.length == 0){
            return;
        }
        int result = roleMapper.insertRoleMenu(roleId,ids);

        if(result != ids.length){
            log.error("【角色授权】:角色授权失败,roleId = {} ,ids = {}",roleId, Arrays.toString(ids));
            throw new HospitalException(HospitalErrorResultEnum.ROLE_SAVE_MENU_ERROR);
        }
    }
}
